package com.zhong.gang.applib.ui.view;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;

import com.zhong.gang.applib.R;
import com.zhong.gang.applib.databinding.LayoutShipperBinding;
import com.zhong.gang.applib.ui.matching.MatchingBean;

public class ShipperView extends FrameLayout {
    private ViewClickedListener listener;
    private LayoutShipperBinding binding;
    private MatchingBean bean;

    public void setListener(ViewClickedListener listener) {
        this.listener = listener;
    }

    public ShipperView(@NonNull Context context) {
        super(context);
        initView();
    }

    public ShipperView(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public ShipperView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView(){
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_shipper,null);
        binding = DataBindingUtil.bind(view);
        binding.setView(this);
        if (binding != null) {
            binding.consignorPayCardNumLayout.setVisibility(GONE);
            binding.consignorBankLayout.setVisibility(GONE);
            binding.consignorPhoneLayout.setVisibility(GONE);
            binding.consignorCardLayout.setVisibility(GONE);
            binding.consignorNameLayout.setVisibility(GONE);
            binding.remarkLayout.setVisibility(GONE);
        }
        this.addView(view);

    }

    public void onViewClicked(View view){
        if (view.getId() == R.id.match) {
            if (listener != null)
                listener.onViewClicked();
        }
    }

    public void setData(MatchingBean bean){
        this.bean = bean;
        if (bean == null || binding ==  null)
            return;
        MatchingBean.ConsignorBean consignorBean = bean.getConsignor();
        MatchingBean.ConsignorBankCardBean consignorBankCard = bean.getConsignorBankCard();
        setData(consignorBean.getName(),consignorBean.getPhone()
                ,consignorBankCard.getIdCardNum(),consignorBankCard.getBankName(),consignorBankCard.getBankCardNum(),bean.getRemark());
    }

    /**
     * 放入货主信息
     */
    public void setData(String consignorName,String consignorPhone,String consignCardStr,String consignBack,String consignorPayCardNum,String remarkStr){
        consignorName = getTextStr(consignorName);
        consignorPhone = getTextStr(consignorPhone);
        consignCardStr = getTextStr(consignCardStr);
        consignBack = getTextStr(consignBack);
        consignorPayCardNum = getTextStr(consignorPayCardNum);
        remarkStr = getTextStr(remarkStr);
        binding.consignorNameLayout.setVisibility(TextUtils.isEmpty(consignorName)?GONE:VISIBLE);
        binding.consignorPhoneLayout.setVisibility(TextUtils.isEmpty(consignorPhone)?GONE:VISIBLE);
        binding.consignorCardLayout.setVisibility(TextUtils.isEmpty(consignCardStr)?GONE:VISIBLE);
        binding.consignorBankLayout.setVisibility(TextUtils.isEmpty(consignBack)?GONE:VISIBLE);
        binding.consignorPayCardNumLayout.setVisibility(TextUtils.isEmpty(consignorPayCardNum)?GONE:VISIBLE);
        binding.remarkLayout.setVisibility(TextUtils.isEmpty(remarkStr)?GONE:VISIBLE);
        binding.consignorName.setText(consignorName);
        binding.consignorPhone.setText(consignorPhone);
        binding.consignorCard.setText(consignCardStr);
        binding.consignorBank.setText(consignBack);
        binding.consignorPayCardNum.setText(consignorPayCardNum);
        binding.remarkTv.setText(remarkStr);
    }

    private String getTextStr(String tv){
        if (TextUtils.isEmpty(tv))
            return "";
        return tv;
    }


    public MatchingBean getBean() {
        return bean;
    }

    public String getRemark(){
        if (binding == null)
            return "";
        return binding.remarkTv.getText().toString();
    }

    public void showEdt(boolean status){
        if (binding != null)
            binding.match.setVisibility(status?VISIBLE:GONE);
    }
}
